// pages/address/select-address/select-address.js
var api = require('../../../api/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consignees: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initShippingAddress();
  },

  selectTap: function (e) {
    let that = this;
    var consignee = e.currentTarget.dataset.id;
    wx.request({
      url: api.AddressSetDefault,
      data: {
        consignee: consignee
      },
      method: "POST",
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: (res) => {
        wx.navigateBack({})
      }
    })
  },

  editAddess: function (e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../new-address/new-address?index=' + index,
    })
  },

  initShippingAddress: function () {
    let that = this;
    wx.request({
      url: api.AddressGet,
      data: {

      },
      method: "POST",
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hide
        if (res.data.error_code == 0) {
          that.setData({
            consignees: res.data.consignees
          })
        }
      }
    })
  },

  addAddess: function () {
    wx.navigateTo({
      url: "/pages/address/new-address/new-address"
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initShippingAddress();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})